# import os, sys
# sys.path.append(os.getcwd())

import json
import pytest
from common.httpRequest import http_post_json
from common.readYaml import read_yaml


class TestWxInterface(object):
    """
    测试微信小程序接口是否正常；
    正常返回的code=000，msg=成功
    :return:
    """
    # 测试数据yaml中是接口url地址和data数据
    conf_inf_data = read_yaml('inf_data_release.yaml')
    test_intfaces = conf_inf_data['protocal'] + conf_inf_data['host']

    @pytest.mark.parametrize('conf_dictionary', conf_inf_data['interfaces'])
    @pytest.mark.wxInterfacesRegression
    def test_wx_interface(self, conf_dictionary):
        print(f"测试接口：{conf_dictionary['comment']}\n"
              f"接口地址：{self.test_intfaces}{conf_dictionary['inf']}")
        # http_post_json 函数接受url，data参数
        resp_json = http_post_json(self.test_intfaces + conf_dictionary['inf'], json.dumps(conf_dictionary['data']))
        assert resp_json['code'] == "000"
        assert resp_json['msg'] == '成功'

    # 写法二：
    # @pytest.mark.wxInterfacesRegression
    # def test_wx_interface(self, conf_read_yaml):
    #     print(f'测试微信小程序各个版块接口是否请求正常：')
    #     for i in range(len(conf_read_yaml['interfaces'])):
    #         resp_json = http_post_json(
    #             conf_read_yaml['protocal'] + conf_read_yaml['host'] + conf_read_yaml['interfaces'][i]['inf'],
    #             json.dumps(conf_read_yaml['interfaces'][i]['data']))
    #
    #         assert resp_json['code'] == "000"
    #         assert resp_json['msg'] == '成功'
    #         print(f"测试接口：{conf_read_yaml['interfaces'][i]['inf']},接口正常")


if __name__ == '__main__':
    pytest.main(['-s', '-m', 'wxInterfacesRegression'])
